/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hidromedsa.measure.controller.filesprocessor;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author spacerverus
 */
public class FileRW {

    String file;
    File tmpFile;

    public FileRW(String f) {
        file = f;
    }

    public List<String> reader() {
        List fileList = new ArrayList();
        LineNumberReader line;
        String read = null;
        try {
            line = new LineNumberReader(new FileReader(file));
            setTmpFile(new File(file));
            boolean ciclo = true;
            while (ciclo) {
                read = line.readLine();
                if (!(read == null)) {
                    fileList.add(read);
                } else {
                    ciclo = false;
                }
            }
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "file not found." + ex, "Error file", JOptionPane.ERROR_MESSAGE);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "file not found." + ex, "Error file", JOptionPane.ERROR_MESSAGE);
        }
        return fileList;
    }

    public boolean write(List<String> info) {
        boolean list = false;
        Object[] arrayObject = info.toArray();
        int lines = 0;
        String file = "";
        for (Object temp : arrayObject) {
            lines++;
            file += temp + "\n";
        }
        if (!write(file)) {
            JOptionPane.showMessageDialog(null, "Could not save the line " + lines + " in file: " + file, "Error file", JOptionPane.ERROR_MESSAGE);
        }
        JOptionPane.showMessageDialog(null, lines + " rows were stored successfully File " + file, "Message file", JOptionPane.INFORMATION_MESSAGE);
        return list;
    }

    public boolean write(String line) {
        boolean list = false;
        int m = line.length();
        FileOutputStream sFile = null;
        int n = 0;
        int dato;
        int x = 0;
        try {
            sFile = new FileOutputStream(file);
            while (n < m) {
                dato = line.charAt(n);
                sFile.write(dato);
                n++;
            }
            sFile.close();
        } catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(null, "file " + file + " not found." + e, "Error file", JOptionPane.ERROR_MESSAGE);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "save error" + e, "Error file", JOptionPane.ERROR_MESSAGE);
        }
        list = true;
        return list;
    }

    public String getFile() {
        return file;
    }

    public void setFile(String file) {
        this.file = file;
    }

    public File getTmpFile() {
        return tmpFile;
    }

    public void setTmpFile(File tmpFile) {
        this.tmpFile = tmpFile;
    } 
}
